package com.warmlight.voicepacket.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Animatable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.bumptech.glide.Glide;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeControllerBuilder;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.imagepipeline.image.ImageInfo;
import com.warmlight.voicepacket.R;
import com.warmlight.voicepacket.base.BaseActivity;
import com.warmlight.voicepacket.bean.comm.PictureBean;
import com.warmlight.voicepacket.service.DownloadUtil;
import com.warmlight.voicepacket.ui.communitylm.widget.CommunityShare;
import com.warmlight.voicepacket.utils.Log;
import com.warmlight.voicepacket.view.picture.NumberIndicater;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import me.relex.photodraweeview.OnPhotoTapListener;
import me.relex.photodraweeview.PhotoDraweeView;

/**
 * Created by weiweijinpeng on 2018/9/12.
 *  图片预览
 *
 */

public class CommDetailsActivity extends BaseActivity implements View.OnClickListener{


    @BindView(R.id.view_pager)
    ViewPager mViewPager;

    @BindView(R.id.commdetails_share)
    ImageView mCommShare;

    @BindView(R.id.commdetails_down)
    ImageView mCommDown;

    @BindView(R.id.loading_progress)
    ProgressBar progressBar;

    @BindView(R.id.number_indicater)
    NumberIndicater mNumberIndicater;

    int mPosition;

    private List<Map<String, Object>> mData=new ArrayList<>();

    List<String> pictureList=new ArrayList<>();

    public static void startCommDetailsActivity(Context context,List<String> pictureList,int position){
        Intent intent=new Intent(context,CommDetailsActivity.class);
        intent.putStringArrayListExtra("pictureList", (ArrayList<String>) pictureList);
        intent.putExtra("position",position);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comm_details);
        initView();
    }


    private void initView() {
        pictureList.addAll(getIntent().getStringArrayListExtra("pictureList"));

        String[] arrat=pictureList.toArray(new String[pictureList.size()]);
        //对数组实例化
        pictureList=new ArrayList<>();

        //对数组进行重新排序，把点击的图片放在第一个位置
        for (int j=0;j<arrat.length;j++){
            Log.e("元素"+arrat[j]);
            if (j==getIntent().getIntExtra("position",0)){
                pictureList.add(0,arrat[j]);
            }else{
                pictureList.add(arrat[j]);
            }
        }

        for (int i=0;i<pictureList.size();i++){

            Map<String, Object> map = new HashMap<>();
            map.put("url", pictureList.get(i));
            map.put("view", new PhotoDraweeView(this));
            mData.add(map);
        }

        mViewPager.setAdapter(mAdapter);
        mNumberIndicater.bindViewPager(mViewPager);
    }

    @OnClick({R.id.commdetails_share,R.id.commdetails_down})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.commdetails_share:

                Map<String,String> map=new HashMap<>();
                map.put("img",pictureList.get(mPosition));
                map.put("type","1");
                CommunityShare.getInstance(map).show(getSupportFragmentManager().beginTransaction()
                        .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE),"ads");
                break;
            case R.id.commdetails_down:
                progressBar.setVisibility(View.VISIBLE);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        progressBar.setVisibility(View.GONE);

                    }
                },3000);
                PictureBean pictureBean=new PictureBean();
                pictureBean.setFileName(pictureList.get(mPosition));
                DownloadUtil.downloadCallMedia(this,pictureBean);
                break;
        }
    }

    private PagerAdapter mAdapter=new PagerAdapter() {
        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            mPosition=position;

            final PhotoDraweeView imageView = (PhotoDraweeView) mData.get(position).get("view");
            //添加点击事件
            imageView.setOnPhotoTapListener(new OnPhotoTapListener() {
                @Override
                public void onPhotoTap(View view, float x, float y) {
                    finish();
                }
            });

            imageView.setScaleType(PhotoDraweeView.ScaleType.FIT_CENTER);

            Glide.with(CommDetailsActivity.this)
                    .load(mData.get(position).get("url").toString())
                    .into(imageView);
            container.addView(imageView, 0);

            //点击图片收缩
            final PipelineDraweeControllerBuilder controller = Fresco.newDraweeControllerBuilder();
            controller.setUri(mData.get(position).get("url").toString());
            controller.setOldController(imageView.getController());
            controller.setControllerListener(new BaseControllerListener<ImageInfo>() {
                @Override
                public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
                    super.onFinalImageSet(id, imageInfo, animatable);
                    if (imageInfo == null || imageView == null) {
                        return;
                    }
                    imageView.update(imageInfo.getWidth(), imageInfo.getHeight());
                }
            });
            imageView.setController(controller.build());

            return mData.get(position).get("view");
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            PhotoDraweeView x = (PhotoDraweeView) mData.get(position).get("view");
            x.setScaleType(PhotoDraweeView.ScaleType.FIT_CENTER);
            container.removeView(x);
        }

    };
}
